package comgutianyu.qihangjiaoyu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

import comgutianyu.qihangjiaoyu.MainActivity;
import comgutianyu.qihangjiaoyu.R;
import comgutianyu.qihangjiaoyu.db.UserDao;
import comgutianyu.qihangjiaoyu.model.User;

public class LoginActivity extends AppCompatActivity {
    private EditText etPhone, etPassword;
    private Button btnLogin;
    private CheckBox cbAgreement;
    private ImageButton btnTogglePassword;
    private boolean isPasswordVisible = false;
    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        userDao = new UserDao(this);
        initViews();
        setListeners();
    }

    private void initViews() {
        etPhone = findViewById(R.id.etPhone);
        etPassword = findViewById(R.id.etPassword);
        btnLogin = findViewById(R.id.btnLogin);
        cbAgreement = findViewById(R.id.cbAgreement);
        btnTogglePassword = findViewById(R.id.btnTogglePassword);
        
        ImageButton btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(v -> onBackPressed());

        TextView tvRegister = findViewById(R.id.tvVerifyCodeLogin);
        tvRegister.setOnClickListener(v -> {
            startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
        });
    }

    @Override
    public void onBackPressed() {
        if (isTaskRoot()) {
            startActivity(new Intent(this, MainActivity.class));
        }
        finish();
    }

    private void setListeners() {
        btnLogin.setOnClickListener(v -> attemptLogin());
        
        btnTogglePassword.setOnClickListener(v -> togglePasswordVisibility());
    }

    private void togglePasswordVisibility() {
        if (isPasswordVisible) {
            etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            btnTogglePassword.setImageResource(R.drawable.ic_visibility_off);
        } else {
            etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            btnTogglePassword.setImageResource(R.drawable.ic_visibility);
        }
        isPasswordVisible = !isPasswordVisible;
        etPassword.setSelection(etPassword.getText().length());
    }

    private void attemptLogin() {
        String phone = etPhone.getText().toString().trim();
        String password = etPassword.getText().toString().trim();

        if (!cbAgreement.isChecked()) {
            Toast.makeText(this, "请先同意服务条款和隐私协议", Toast.LENGTH_SHORT).show();
            return;
        }

        if (phone.isEmpty()) {
            etPhone.setError("请输入手机号");
            return;
        }

        if (password.isEmpty()) {
            etPassword.setError("请输入密码");
            return;
        }

        User user = userDao.getUserByPhone(phone);
        if (user != null && user.getPassword().equals(password)) {
            getSharedPreferences("user_prefs", MODE_PRIVATE)
                    .edit()
                    .putBoolean("is_logged_in", true)
                    .putString("current_phone", phone)
                    .apply();

            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
            
            Intent intent = new Intent(this, PersonalInfoActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            finish();
        } else {
            Toast.makeText(this, "手机号或密码错误", Toast.LENGTH_SHORT).show();
        }
    }
} 